<?php
/**
 * FCJ EuroDisney Form database library
*
* @package FCJ\Database
*
*
* @author Felipe
*/
namespace FCJ\Database;

require_once ('php_Util_DB/php_Util_DB_PDO_lib.php');


/**
 * Namespace for PHP Util Database library
 *
*/
use php_Util\DB as ns_php_Util_DB;


use php_Util as ns_php_Util;

/**
 * Namespace for Database library
 *
 */
use FCJ\Database as ns_FCJ_Database;


/**
 * FCJ EuroDisney form constants
 *
 */
class FCJ_DB_EuroDisney_Form_CONSTANTS {
	
	const SUBMIT_RESULT__ERROR__BAD_PARAMETERS = 2001;
	const SUBMIT_RESULT__ERROR__NAME_DUPLICATED = 2002;
	const SUBMIT_RESULT__ERROR__USER_LINK = 2003;
	const SUBMIT_RESULT__ERROR__GROUP_DATA = 2004;
	const SUBMIT_RESULT__ERROR__MEMBER_DATA = 2005;
	
	const SUBMIT_RESULT__ERROR__OTHER = 2999;
}


/**
 * FCJ EuroDisney form table
 *
 */

class FCJ_DB_EuroDisney_Form_TABLE {
	public $tableName = 'fcj_EuroDisney_Form';
	public $field_id = 'id';
	public $field_time_at_insert = 'time_at_insert';
	public $field_remote_IP = 'remote_IP';
	public $field_remote_browser_DATA = 'remote_browser_DATA';
	public $field_extra_DATA = 'extra_DATA';
	public $field_submit_Result = 'submit_Result';
	public $field_related_ID = 'related_ID';
	public $field_group_name = 'group_name';
	public $field_group_email = 'group_email';
	public $field_group_phone = 'group_phone';
	public $field_group_user_Link_ID = 'group_user_Link_ID';
	public $field_group_Payment_Method = 'group_Payment_Method';
	public $field_group_Transport_Method = 'group_Transport_Method';
	public $field_members_DATA = 'members_DATA';
}




/**
 * FCJ EuroDisney form data
 *
 */
class FCJ_DB_EuroDisney_Form_DATA {
	public $id = null;
	public $time_at_insert = null;
	public $remote_IP = null;
	public $remote_browser_DATA = null;
	public $extra_DATA = null;
	public $submit_Result = null;
	public $related_ID = null;
	public $group_name = null;
	public $group_email = null;
	public $group_phone = null;
	public $group_user_Link_ID = null;
	public $group_Payment_Method = null;
	public $group_Transport_Method = null;
	public $members_DATA = null;
}



/**
 * FCJ EuroDisney form editor.
 * 
 * Provides CRUD facilities
 * 
 *
 */
class FCJ_DB_EuroDisney_Form_EDITOR extends ns_php_Util_DB\php_Util_DB_EDITOR_PDO {

	/**
	 * @var FCJ_DB_EuroDisney_Form_TABLE
	 */
	protected $table_Description = null;
	
	
	public function get_Description_Table() {
		return $this->table_Description;
	}
	
	
	public function __construct($table_Description = null) {
	
		if ($table_Description != null) {
			$this->table_Description = $table_Description;
		} else {
			$this->table_Description = ns_FCJ_Database\FCJ_DB_EuroDisney_Form_FACTORY::get_NEW__FCJ_DB_EuroDisney_Form_TABLE();
		}
	}
	
	
	/**
	 * Return array of insert fields for SQL insert
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 */
	protected function get_Create_Fields($object_DATA) {
	
		$insertFields = array();
		
		$object_DATA->id !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_id => $object_DATA->id)) : null;
		$object_DATA->time_at_insert !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_time_at_insert => $object_DATA->time_at_insert)) : null;
		$object_DATA->remote_IP !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_remote_IP => $object_DATA->remote_IP)) : null;
		$object_DATA->remote_browser_DATA !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_remote_browser_DATA => $object_DATA->remote_browser_DATA)) : null;
		$object_DATA->extra_DATA !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_extra_DATA => $object_DATA->extra_DATA)) : null;
		$object_DATA->submit_Result !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_submit_Result => $object_DATA->submit_Result)) : null;
		$object_DATA->related_ID !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_related_ID => $object_DATA->related_ID)) : null;
		$object_DATA->group_name !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_name => $object_DATA->group_name)) : null;
		$object_DATA->group_email !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_email => $object_DATA->group_email)) : null;
		$object_DATA->group_phone !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_phone => $object_DATA->group_phone)) : null;
		$object_DATA->group_user_Link_ID !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_user_Link_ID => $object_DATA->group_user_Link_ID)) : null;
		$object_DATA->group_Payment_Method !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_Payment_Method => $object_DATA->group_Payment_Method)) : null;
		$object_DATA->group_Transport_Method !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_group_Transport_Method => $object_DATA->group_Transport_Method)) : null;
		$object_DATA->members_DATA !== null ? ns_php_Util\php_Util::array_push_associative($insertFields, array($this->table_Description->field_members_DATA => $object_DATA->members_DATA)) : null;

		return $insertFields;
	}
	
	
	/**
	 * Return array of update fields for SQL insert
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 */
	protected function get_Update_Fields($object_DATA) {
		
		$object_DATA_Update = clone $object_DATA;
		
		$object_DATA_Update->id = null;	// ID is a key. No update for this field. 
		$updateFields = $this->get_Create_Fields($object_DATA_Update);
		
		return $updateFields;
	}
	
	
	/**
	 * Read DATA from row array
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 * @param array $row_Array
	 */
	protected function get_DATA_From_RowArray($object_DATA, $row_Array) {

		$object_DATA->id = $row_Array[$this->table_Description->field_id];
		$object_DATA->time_at_insert = $row_Array[$this->table_Description->field_time_at_insert];
		$object_DATA->remote_IP = $row_Array[$this->table_Description->field_remote_IP];
		$object_DATA->remote_browser_DATA = $row_Array[$this->table_Description->field_remote_browser_DATA];
		$object_DATA->extra_DATA = $row_Array[$this->table_Description->field_extra_DATA];
		$object_DATA->submit_Result = $row_Array[$this->table_Description->field_submit_Result];
		$object_DATA->related_ID = $row_Array[$this->table_Description->field_related_ID];
		$object_DATA->group_name = $row_Array[$this->table_Description->field_group_name];
		$object_DATA->group_email = $row_Array[$this->table_Description->field_group_email];
		$object_DATA->group_phone = $row_Array[$this->table_Description->field_group_phone];
		$object_DATA->group_user_Link_ID = $row_Array[$this->table_Description->field_group_user_Link_ID];
		$object_DATA->group_Payment_Method = $row_Array[$this->table_Description->field_group_Payment_Method];
		$object_DATA->group_Transport_Method = $row_Array[$this->table_Description->field_group_Transport_Method];
		$object_DATA->members_DATA = $row_Array[$this->table_Description->field_members_DATA];
		
	}
	
	
	/**
	 * CRUD method for CREATE object
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 * 
	 */
	public function CRUD__Create($object_DATA) {
		
		// Check required parameters -----------------------------------------------------------------------|\/|---
		
		if (!is_a($object_DATA, "FCJ\Database\FCJ_DB_EuroDisney_Form_DATA")) {
			
			throw new \Exception("FCJ_DB_EuroDisney_Form_DATA is required.");
		}
		
		// -------------------------------------------------------------------------------------------------|/\|---
		
		
		// Create object -------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->create_Object($this->table_Description->tableName, $this->get_Create_Fields($object_DATA));
		
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot create object.");
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---
		
		
		// Insert last insert ID in object ------------------------------------------|-|---
		$object_DATA->id = $this->db_Connection->_connection_Link->lastInsertId();
		
		
		return $affected_Rows;
	}
	
	
	/**
	 * CRUD method for READ object
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 * @throws \Exception
	 *
	 */
	public function CRUD__Read($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_EuroDisney_Form_DATA')) {
			throw new \Exception("FCJ_DB_EuroDisney_Form_DATA is required.");
		}
	
		// -------------------------------------------------------------------------------------------------|/\|---

		
		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
				);
		// ------------------------------------------------------------------------|/\|---
		
		
		
		// Read object ---------------------------------------------------------------------------------------------------|\/|---
		try {
			
			$result_DATA = $this->read_Object($this->table_Description->tableName, $conditions_Array);
			$this->get_DATA_From_RowArray($object_DATA, $result_DATA[0]);
			
		} catch (Exception $e) {
			throw new \Exception("Error reading object. ". $e->getMessage());
		}
		// ---------------------------------------------------------------------------------------------------------------|/\|---
		
	}
	
	

	/**
	 * CRUD method for UPDATE object
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Update($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_EuroDisney_Form_DATA')) {
			throw new \Exception("FCJ_DB_EuroDisney_Form_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---
		

		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---
		
		
		// Update object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->update_Object($this->table_Description->tableName, $this->get_Update_Fields($object_DATA), $conditions_Array);
	
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot update object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---
		
	
		return $affected_Rows;
	}
	
	
	

	/**
	 * CRUD method for DELETE object
	 * @param FCJ_DB_EuroDisney_Form_DATA $object_DATA
	 * @throws \Exception
	 * @return int affected rows
	 *
	 */
	public function CRUD__Delete($object_DATA) {
	
		// Check required parameters -----------------------------------------------------------------------|\/|---
		if (!is_a($object_DATA, 'FCJ\Database\FCJ_DB_EuroDisney_Form_DATA')) {
			throw new \Exception("FCJ_DB_EuroDisney_Form_DATA is required.");
		}
		// -------------------------------------------------------------------------------------------------|/\|---
	
	
		// Prepare conditions -----------------------------------------------------|\/|---
		$conditions_Array = array(
				$this->table_Description->field_id => $object_DATA->id
		);
		// ------------------------------------------------------------------------|/\|---
	
	
		// Delete object ---------------------------------------------------------------------------------------------------------------------------|\/|---
		$affected_Rows = $this->delete_Object($this->table_Description->tableName, $conditions_Array);
	
		if ( $affected_Rows < 1 ) {
			throw new \Exception("Cannot delete object.");
		}
		// -----------------------------------------------------------------------------------------------------------------------------------------|/\|---
	
	
		return $affected_Rows;
	}
	
	
	
}


/**
 * 
 * FCJ EuroDiseny Form Database Factory
 * 
 * Provides facilities for create EuroDiseny Form related objects
 * 
 * @author Felipe
 *
 */
class FCJ_DB_EuroDisney_Form_FACTORY {


	public static function get_NEW__FCJ_DB_EuroDisney_Form_TABLE() {
		return new FCJ_DB_EuroDisney_Form_TABLE();
	}

	public static function get_NEW__FCJ_DB_EuroDisney_Form_DATA() {
		return new FCJ_DB_EuroDisney_Form_DATA();
	}

	
	public static function get_NEW__FCJ_DB_EuroDisney_Form_EDITOR() {
		return new FCJ_DB_EuroDisney_Form_EDITOR();
	}
	
}


